define(function (require) {

    var $ = require("zepto");
    var global = require("global");

    var page = {
        _page: $(".m-page"),
        _pageNum: $(".m-page").size(),
        _pageNow: 0,
        _pageNext: null,
        _touchStartValY: 0,
        _touchDeltaY: 0,
        _moveStart: true,
        _movePosition: null,
        _movePosition_c: null,
        _mouseDown: false,
        _moveFirst: true,
        _moveInit: false,
        _firstChange: false,
        page_start: function () {
            page._page.on("touchstart mousedown", page.page_touch_start);
            page._page.on("touchmove mousemove", page.page_touch_move);
            page._page.on("touchend mouseup", page.page_touch_end);
        },
        page_stop: function () {
            page._page.off("touchstart mousedown");
            page._page.off("touchmove mousemove");
            page._page.off("touchend mouseup");
        },
        page_touch_start: function (event) {
            if (page._moveStart) {
                if ("touchstart" === event.type) {
                    page._touchStartValY = window.event.touches[0].pageY;
                } else {
                    page._touchStartValY = event.pageY || event.y;
                    page._mouseDown = true;
                }
                page._moveInit = true;
                global._handleEvent("start");
            }
        },
        page_touch_move: function (event) {
            event.preventDefault();
            if (page._moveStart && page._moveInit) {
                var posY;
                var pageNow = page._page.eq(page._pageNow);
                if ("touchmove" === event.type) {
                    posY = window.event.touches[0].pageY;
                } else {
                    if (!page._mouseDown) {
                        return;
                    }
                    posY = event.pageY || event.y;
                }
                page.page_translate(page.page_position(event, posY, pageNow));
                global._handleEvent("move");
            }
        },
        page_position: function (t, a, o) {
            function r(t) {
                var a, o, r = global._translateZ();
                page._page.removeClass("action"), $(t[1]).addClass("action").removeClass("f-hide"), page._page.not(".action").addClass("f-hide"), $(t[0]).removeClass("f-hide").addClass("active"), "up" == page._movePosition ? (a = parseInt($(window).scrollTop()), o = a > 0 ? $(window).height() + a : $(window).height(), t[0].style[global._prefixStyle("transform")] = "translate(0," + o + "px)" + r, $(t[0]).attr("data-translate", o)) : (t[0].style[global._prefixStyle("transform")] = "translate(0,-" + Math.max($(window).height(), $(t[0]).height()) + "px)" + r, $(t[0]).attr("data-translate", -Math.max($(window).height(), $(t[0]).height()))), $(t[1]).attr("data-translate", 0), page._page.eq(page._pageNext).height($(window).height())
            }
            var s, l;
            if ("undefined" != a && (page._touchDeltaY = a - page._touchStartValY), page._movePosition = a - page._touchStartValY > 0 ? "down" : "up", page._movePosition != page._movePosition_c ? (page._moveFirst = true, page._movePosition_c = page._movePosition) : page._moveFirst = false, 0 >= page._touchDeltaY)
                page._pageNext = 0 == o.next(".m-page").length ? 0 : page._pageNow + 1, l = page._page.eq(page._pageNext)[0];
            else {
                if (0 == o.prev(".m-page").length) {
                    if (!page._firstChange)
                        return page._pageNext = null, page._touchDeltaY = 0, void 0;
                    page._pageNext = page._pageNum - 1
                } else
                    page._pageNext = page._pageNow - 1;
                l = page._page.eq(page._pageNext)[0]
            }
            return s = page._page.eq(page._pageNow)[0], node = [l, s], page._moveFirst && r(node), node
        },
        page_translate: function (t) {
            if (t) {
                var a, o, r, s = global._translateZ(), l = page._touchDeltaY;
                $(t[0]).attr("data-translate") && (a = l + parseInt($(t[0]).attr("data-translate"))), t[0].style[global._prefixStyle("transform")] = "translate(0," + a + "px)" + s, $(t[1]).attr("data-translate") && (o = l + parseInt($(t[1]).attr("data-translate"))), r = (1 - Math.abs(.2 * l / $(window).height())).toFixed(6), o /= 5, t[1].style[global._prefixStyle("transform")] = "translate(0," + o + "px)" + s + " scale(" + r + ")"
            }
        },
        page_touch_end: function () {
            page._moveInit = false;
            page._mouseDown = false;
            page._moveStart && (page._pageNext || 0 == page._pageNext) && (page._moveStart = false, page._moveFirst = true, Math.abs(page._touchDeltaY) > 10 && (page._page.eq(page._pageNext)[0].style[global._prefixStyle("transition")] = "all .3s", page._page.eq(page._pageNow)[0].style[global._prefixStyle("transition")] = "all .3s"), Math.abs(page._touchDeltaY) >= 100 ? page.page_success() : Math.abs(page._touchDeltaY) > 10 && 100 > Math.abs(page._touchDeltaY) ? page.page_fial() : page.page_fial(), global._handleEvent("end"), page._movePosition = null, page._movePosition_c = null, page._touchStartValY = 0)
        },
        page_success: function () {
            var tz = global._translateZ();
            page._page.eq(page._pageNext)[0].style[global._prefixStyle("transform")] = "translate(0,0)" + tz;
            var a = page._touchDeltaY > 0 ? $(window).height() / 5 : -$(window).height() / 5, o = .8;
            page._page.eq(page._pageNow)[0].style[global._prefixStyle("transform")] = "translate(0," + a + "px)" + tz + " scale(" + o + ")";
            global._handleEvent("success");
        },
        page_fial: function () {
            var t = global._translateZ();
            return page._pageNext || 0 == page._pageNext ? (page._page.eq(page._pageNext)[0].style[global._prefixStyle("transform")] = "up" == page._movePosition ? "translate(0," + $(window).height() + "px)" + t : "translate(0,-" + $(window).height() + "px)" + t, page._page.eq(page._pageNow)[0].style[global._prefixStyle("transform")] = "translate(0,0)" + t + " scale(1)", global._handleEvent("fial"), void 0) : (page._moveStart = true, page._moveFirst = true, void 0)
        },
        height_auto: function (t, n) {
            n = n ? n : $(window).height();
            t.children(".page-con").css("height", n);
        }};

    return  page;
});